package com.eds.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    // 添加日志对象
    private static final Logger log = LoggerFactory.getLogger(RabbitMQConfig.class);

    @Bean
    public Queue courseImageQueue() {
        // 定义队列参数
        Map<String, Object> args = new HashMap<>();
        // 设置消息过期时间（1小时）
        args.put("x-message-ttl", 3600000);
        // 设置死信交换机
        args.put("x-dead-letter-exchange", "course.dead.exchange");
        // 设置死信路由键
        args.put("x-dead-letter-routing-key", "course.image.dead");

        return new Queue("course.image.queue", true, false, false, args);
    }

    @Bean
    public DirectExchange courseExchange() {
        return new DirectExchange("course.exchange", true, false);
    }

    @Bean
    public Binding bindingCourseImageQueue() {
        return BindingBuilder.bind(courseImageQueue())
                .to(courseExchange())
                .with("course.image");
    }

    // 死信队列和交换机配置
    @Bean
    public Queue deadLetterQueue() {
        return new Queue("course.image.dead.queue", true);
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("course.dead.exchange", true, false);
    }

    @Bean
    public Binding bindingDeadLetterQueue() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with("course.image.dead");
    }

    // 消息转换器
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 配置RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());

        // 消息发送到交换机确认
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                log.error("消息发送到交换机失败：{}", cause);
            }
        });

        // 使用setReturnCallback而不是setReturnsCallback
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            log.error("消息发送到队列失败：exchange={}, routingKey={}, replyCode={}, replyText={}",
                    exchange, routingKey, replyCode, replyText);
        });

        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
}